<template>
	<div>
		<el-form
			:model="dynamicValidateForm"
			ref="dynamicValidateForm"
			label-width="0px"
			class="demo-dynamic"
		>
			<el-form-item
				v-for="(domain, index) in dynamicValidateForm.domains"
				:label="''"
				:key="domain.key"
				:prop="'domains.' + index + '.value'"
				:rules="{
					required: true,
					message: '数据不能为空',
					trigger: 'blur'
				}"
			>
				<!-- <el-input v-model="domain.value"></el-input
				> -->
				<el-input-number
					v-model="domain.value"
					controls-position="right"
					@change="handleChange"
					size="mini"
					:min="0"
					:step="1"
					:precision="0"
					:max="9999999"
				></el-input-number>
				<el-button type="text" @click.prevent="removeDomain(domain)">删除</el-button>
			</el-form-item>
			<el-form-item>
				<!-- <el-button type="primary" @click="submitForm('dynamicValidateForm')"
					>提交</el-button
				> -->
				<el-button style="margin-left:15px;margin-top:6px" @click="addDomain"
					>新增</el-button
				>
				<!-- <el-button @click="resetForm('dynamicValidateForm')">重置</el-button> -->
			</el-form-item>
		</el-form>
	</div>
</template>
<script>
export default {
	data() {
		return {
			dynamicValidateForm: {
				domains: [
					{
						value: ''
					}
				],
				email: ''
			},
			reset: true
		};
	},
	props: {
		value: [String, Number],
		addLimti: [String, Number]
	},
	watch: {
		value(val) {
			if (!this.reset) {
				return;
			}
			this.dynamicValidateForm.domains = [];
			val.split(',').forEach((item, index) => {
				this.dynamicValidateForm.domains.push({
					value: item,
					key: Date.now() + index
				});
			});
		}
	},
	methods: {
		submitForm(formName) {
			this.$refs[formName].validate(valid => {
				if (valid) {
					alert('submit!');
				} else {
					console.log('error submit!!');
					return false;
				}
			});
		},
		resetForm(formName) {
			this.$refs[formName].resetFields();
		},
		removeDomain(item) {
			var index = this.dynamicValidateForm.domains.indexOf(item);
			if (index !== -1) {
				this.dynamicValidateForm.domains.splice(index, 1);
			}
			this.handleChange();
		},
		handleChange() {
			let str = '';
			this.dynamicValidateForm.domains.forEach(item => {
				if (this.dynamicValidateForm.domains.length == 1) {
					str = item.value;
					return;
				}
				str += item.value + ',';
			});
			this.reset = false;
			if (this.dynamicValidateForm.domains.length > 1) {
				this.$emit('input', str.substr(0, str.length - 1));
				return;
			} else {
				this.$emit('input', str);
			}
		},
		addDomain() {
			if (this.addLimti) {
				let length = this.dynamicValidateForm.domains.length;
				if (length <= this.addLimti) {
					this.dynamicValidateForm.domains.push({
						value: '',
						key: Date.now()
					});
				}
				return false;
			}
			this.dynamicValidateForm.domains.push({
				value: '',
				key: Date.now()
			});
		}
	}
};
</script>
<style scoped lang="scss">
::v-deep .el-input-number.is-controls-right .el-input__inner {
	padding-left: 15px;
	padding-right: 25px;
}
::v-deep .el-input-number--mini {
	width: 90px;
	margin-right: 10px;
}
.demo-dynamic {
	display: flex;
	flex-wrap: wrap;
}
</style>
